package com.itheima.service.impl;


import com.alibaba.druid.sql.visitor.functions.Char;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.City;
import com.itheima.dto.CityDTO;
import com.itheima.dto.TotalCityDTO;
import com.itheima.entity.Cities;
import com.itheima.mapper.CitiesMapper;
import com.itheima.service.ICitiesService;
import com.itheima.tool.PingYinUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;


/**
* cities 相关service实现类
* @author WEI
* @since 2021-8-6 15:38:56
*/
@Service
public class CitiesServiceImpl  implements ICitiesService {

@Autowired
private CitiesMapper citiesMapper;


    @Override
    public TotalCityDTO getAllCity() throws BadHanyuPinyinOutputFormatCombination {
        List<City> city1 = citiesMapper.selectListByType();
        //去垃圾
        List<City> citys = new ArrayList<>(500);
        for(City c : city1){
            if(c.getName().length()==0){
                continue;
            }
            citys.add(c);
        }

        Map<Character,List<City>> cityCharacterMap= new HashMap<>(26);

        for(City city : citys) {
            //得到首字母叫什么
            String res = PingYinUtils.getFirstLettersUp(city.getName());
            if(cityCharacterMap.get(res.charAt(0))==null){
                //做一个同key的聚合
                List<City> list = new ArrayList<>(20);
                list.add(city);
                cityCharacterMap.put(res.charAt(0),list);
            }else {
                List<City> list = cityCharacterMap.get(res.charAt(0));
                list.add(city);
            }
        }
        //紧接着，一个List<DTO>来给大的city排个序
        List<CityDTO>  cityDTOS= makeSortedListCityDTO(cityCharacterMap);
        for(CityDTO cityDTO : cityDTOS){
            Collections.sort(cityDTO.getCitys(), new Comparator<City>() {
                @Override
                public int compare(City o1, City o2) {
                    return PingYinUtils.getAllLettersUp(o1.getName())
                            .compareTo(PingYinUtils.getAllLettersUp
                                    (o2.getName()));
                }
            });
        }
        TotalCityDTO totalCityDTO = new TotalCityDTO(cityDTOS);
        return totalCityDTO;
    }

    @Override
    public List<Cities> getCities() {
        QueryWrapper<Cities> queryWrapper = new QueryWrapper();
        queryWrapper.eq("type",1);
        return citiesMapper.selectList(queryWrapper);
    }

    private List<CityDTO> makeSortedListCityDTO(Map<Character, List<City>> cityCharacterMap) {

        List<CityDTO> list = new ArrayList<>(26);
        for(Map.Entry<Character, List<City>> entry : cityCharacterMap.entrySet()){
            CityDTO cityDTO = new CityDTO();
            cityDTO.setName(String.valueOf(entry.getKey()));
            cityDTO.setCitys(entry.getValue());
            list.add(cityDTO);
        }
        return list;
    }


}